package algorithm;

public class Fibonacci {

    public static void main(final String[] args) {

        Fibonacci fibonacci = new Fibonacci();

        for (int i = 1; i <= 20; i++) {
            int fibonacciNumber = fibonacci.getFibonacciNumber(i);
            System.out.println(String.format("The %d fibonacci number is: %d", i, fibonacciNumber));
        }
    }

    private int getFibonacciNumber(final int index) {

        if (index == 1 || index == 2) {
            return 1;
        }
        int previousNumber = 1;
        int currentNumber = 1;
        int temp = 0;
        for (int i = 2; i < index; i++) {
            temp = previousNumber + currentNumber;
            previousNumber = currentNumber;
            currentNumber = temp;
        }
        return temp;
    }

}
